package com.zhixun.mobile.modules.member.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.boxin.framework.base.dao.DaoHelper;
import com.boxin.ims.modules.recharge.entity.RechargeCache;
import com.boxin.ims.modules.wechat.entity.LangType;
import com.boxin.ims.modules.wechat.entity.Lottery;
import com.boxin.ims.modules.wechat.entity.WechatMember;
import com.boxin.ims.modules.wechat.service.SignupConfigService;
import com.boxin.ims.modules.wechat.utils.WechatInterfaceUtils;
import com.boxin.ims.modules.wechat.utils.WechatMenuUtils;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.zhixun.mobile.sys.utils.AssertUtils;

/**
 * @author Jakemanse
 * @date 2014-2-26
 * @function <p>
 * 
 *           </p>
 */
@Service
public class MemberService {

	@Autowired
	SignupConfigService signupConfigService;

	@Autowired
	DaoHelper daoHelper;

	public List<Map<String, Object>> findMemberList() {
		return (List<Map<String, Object>>) daoHelper.queryForList(
				"com.zhixun.mobile.member.selectSyllabusMaxWeek", "");
	}

	/**
	 * @author songyan
	 * @time 2014-2-28 根据WechatId 查询 WechatMembe
	 */
	public WechatMember selectWechatMemberByWechatId(String mobile,
			String wechatId) {
		WechatMember wechatMember = new WechatMember();

		Map<String, Object> map = new HashMap<String, Object>();

		map.put("mobile", mobile);
		map.put("wechatId", wechatId);

		List<Map<String, Object>> list = daoHelper.queryForList(
				"com.zhixun.mobile.member.selectWechatMemberByWechatId", map);

		if (AssertUtils.notNull(list)) {
			wechatMember = (WechatMember) list.get(0);
		}

		return wechatMember;
	}

	/**
	 * @author songyan
	 * @time 2014-2-28 根据Mobile 查询 WechatMembe
	 */
	public List<Map<String, Object>> selectWechatMemberByMobile(Long id,
			String mobile) {
		Map<String, Object> map = new HashMap<String, Object>();

		map.put("userId", id);
		map.put("mobile", mobile);

		List<Map<String, Object>> list = daoHelper.queryForList(
				"com.zhixun.mobile.member.selectWechatMemberByMobile", map);

		return list;
	}

	/**
	 * @author songyan
	 * @time 2014-2-28 注册 wp_wechat_member
	 */
	public void regMember(Map<String, Object> map) {

		daoHelper.insert("com.zhixun.mobile.member.regMember", map);
	}

	/**
	 * @author songyan
	 * @time 2014-2-28 根据user_id 查询 wp_user_define
	 */
	public List<Map<String, Object>> selectUserDefineByuserID(Long id) {
		Map<String, Object> map = new HashMap<String, Object>();

		map.put("userId", id);

		List<Map<String, Object>> list = daoHelper.queryForList(
				"com.zhixun.mobile.member.selectUserDefineByuserID", map);

		return list;
	}

	/**
	 * @author songyan
	 * @time 2014-2-28 注册 wp_lottery_record
	 */
	public void reglotteryRecord(Map<String, Object> map) {

		// 生成二维码 begin--------
		// int width = QRCodeUtils.WIDTH;
		// int height =QRCodeUtils.HEIGHT;
		// //生成二维码
		// QRCode qrcode = new QRCode();
		// String content =
		// QRCodeUtils.generateLotterySN(lotteryRecord.getUser().getId());
		// qrcode.setContent(content);
		// String path = Constants.getWechatResourceSavePath();
		// path += DateUtils.getTodayString()+File.separator+content;
		// FileUtils.createDirectory(path);
		// QRCodeUtils.generateQRCodePNG(content, path, width,height);
		// qrcode.setFileName(content+".png");
		// qrcode.setPngPath(path+".png");
		// qrCodeService.save(qrcode); //保存二维码
		//
		// lotteryRecord.setQrCode(qrcode);

		// 生成二维码 end--------

		// lotteryRecordDao.save(lotteryRecord);

		daoHelper.insert("com.zhixun.mobile.member.insertlotteryRecord", map);
	}

	/**
	 * @author songyan
	 * @time 2014-3-4 根据用户ID 查询 sys_user 商家表 interface_flag 是否开通高级接口
	 * 
	 * @return 0 未开通
	 * @return 1 已开通
	 * 
	 */
	public Boolean selectInterfaceFlagByuserID(Long uid) {
		Boolean b = false;
		Map<String, Object> map = new HashMap<String, Object>();

		map.put("id", uid);

		List<Map<String, Object>> list = daoHelper.queryForList(
				"com.zhixun.mobile.member.selectInterfaceFlagByuserID", map);

		if (AssertUtils.notNull(list)) {
			if (AssertUtils.notNull(list.get(0).get("interface_flag"))) {
				b = (Boolean) list.get(0).get("interface_flag");
			}

		}

		return b;
	}

	/**
	 * @author songyan
	 * @throws Exception
	 * @time 2014-3-4
	 * @param uid
	 *            用户Id
	 * @param oponId
	 *            用户ID
	 * 
	 * 
	 *            根据用户ID 查询 wp_wechat app_id，app_secret 字段 当 app_id，app_secret
	 *            都不为空时 调用 com.boxin.ims.modules.wechat.utils.WechatMenuUtils 类
	 *            getAccessToken(String appid, String secret) 方法 得到 accessToke
	 * 
	 *            调用 com.boxin.ims.modules.wechat.utils.WechatInterfaceUtils 类
	 *            getUserMessage() 方法 得到 WechatMember 对象
	 * 
	 * 
	 * 
	 */
	public WechatMember operateInterface(Long uid, String oponId)
			throws Exception {
		WechatMember member = null;
		Map<String, Object> map = new HashMap<String, Object>();

		map.put("id", uid);

		List<Map<String, Object>> list = daoHelper.queryForList(
				"com.zhixun.mobile.member.selectwpWechatgByuserID", map);
		if (AssertUtils.notNull(list)) {
			if (AssertUtils.notNull(list.get(0).get("app_id"))
					&& AssertUtils.notNull(list.get(0).get("app_secret"))) {

				String accessToke = WechatMenuUtils.getAccessToken(
						((String) list.get(0).get("app_id")), ((String) list
								.get(0).get("app_secret")));

				member = WechatInterfaceUtils.getUserMessage(accessToke,
						oponId, LangType.zh_CN);
			}
		}
		return member;

	}

	/**
	 * 获得秒杀的列表
	 * 
	 * @author xiaoqian
	 * @param id
	 * @return 2014-3-7上午10:56:52
	 */
	public List<Map<String, Object>> selectSeckillGainerBymemberID(Long memberID) {
		Map<String, Object> map = new HashMap<String, Object>();

		map.put("memberID", memberID);

		List<Map<String, Object>> list = daoHelper.queryForList(
				"com.zhixun.mobile.member.selectSeckillGainerBymemberID", map);
		return list;
	}

	/**
	 * 根据会员ID 查询会员奖品
	 */
	public List<Map<String, Object>> myprize(Long id) {
		Map<String, Object> map = new HashMap<String, Object>();

		map.put("member_id", id);
		List<Map<String, Object>> list = daoHelper.queryForList(
				"com.zhixun.mobile.member.selectlotteryRecordBymemberID", map);

		return list;
	}

	/**
	 * 根据推荐码查询推荐会员
	 */

	public WechatMember selectMemberByRecommendCode(String recommendCode) {
		WechatMember wechatMember = new WechatMember();
		Map<String, Object> map = new HashMap<String, Object>();

		map.put("recommend_code", recommendCode);

		List<Map<String, Object>> list = daoHelper.queryForList(
				"com.zhixun.mobile.member.selectMemberByRecommendCode", map);

		if (AssertUtils.notNull(list)) {
			if (list.size() > 0) {
				wechatMember.setName(list.get(0).get("name").toString());

				wechatMember.setId(Long.parseLong(list.get(0).get("id")
						.toString()));
				wechatMember.setScore((Integer) list.get(0).get("score"));

				wechatMember.setMoney((Integer) list.get(0).get("money"));

				User user = new User();

				user.setId(Long
						.parseLong(list.get(0).get("user_id").toString()));

				wechatMember.setUser(user);

				wechatMember.setWechatId(list.get(0).get("wechat_id")
						.toString());

			} else {
				wechatMember = null;
			}

		} else {
			wechatMember = null;
		}

		return wechatMember;
	}

	/**
	 * 根据会员ID跟新会员表score 对父会员进行操作
	 */
	public void updateMemberByID(WechatMember wechatMember) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("score", wechatMember.getScore()); // 积分进行+5
		map.put("id", wechatMember.getId());
		map.put("money", wechatMember.getMoney());

		daoHelper.update("com.zhixun.mobile.member.updateMemberByID", map);
	}

	/**
	 * 查看我的推荐会员
	 */
	public List<RechargeCache> selectMyCommend(WechatMember member) {

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("member_id", member.getId());

		List<RechargeCache> mycomendList = daoHelper.queryForList(
				"com.zhixun.mobile.member.selectRechargeCacheByMemberID", map);

		// // 查询我的推荐会员 只查一级 begin
		// Map<String, Object> map = new HashMap<String, Object>();
		// map.put("recommend_code", member.getRecommendCode());
		//
		// List<WechatMember> mycomendList = daoHelper
		// .queryForList(
		// "com.zhixun.mobile.member.selectrecommendMemberByrecommendCode",
		// map);
		// // 查询我的推荐会员 只查一级 end
		//
		// // 根据登陆的会员ID 查询 会员积分表 查询所有积分记录 begin
		// Map<String, Object> map2 = new HashMap<String, Object>();
		// map2.put("id", member.getId());
		// map2.put("remark", "%父推荐人加%");
		//
		// List<MemberScore> scorelist = daoHelper.queryForList(
		// "com.zhixun.mobile.member.selectMemberScoreByMID", map2);
		// // 根据登陆的会员ID 查询 会员积分表 查询所有积分记录 end
		//
		// // 对推荐会员list, 进行判断 积分， 余额
		//
		// if (AssertUtils.notNull(mycomendList) && mycomendList.size() > 0) {
		// for (int i = 0; i < mycomendList.size(); i++) {
		// mycomendList.get(i).setMoney(0);
		// mycomendList.get(i).setScore(0);
		// }
		// if (AssertUtils.notNull(scorelist) && scorelist.size() > 0) {
		// for (int i = 0; i < scorelist.size(); i++) {
		// for (int j = 0; j < mycomendList.size(); j++) {
		// String[] strings1 = scorelist.get(i).getRemarks()
		// .split(",");
		// String[] strings2 = strings1[1].split(":");
		//
		// if (strings2[1].equals(mycomendList.get(j).getId()
		// .toString())) {
		//
		// if (strings1[0].equals("父推荐人加余额")) {
		//
		// mycomendList.get(j).setMoney(
		// scorelist.get(i).getScore());
		//
		// }
		// if (strings1[0].equals("父推荐人加积分")) {
		// mycomendList.get(j).setScore(
		// scorelist.get(i).getScore());
		// }
		// }// if (strings2[1].equals(mycomendList.get(i).getId())
		// }// for (int j = 0; j < mycomendList.size(); j++)
		// }// for (int i = 0; i < scorelist.size(); i++)
		// }// if (AssertUtils.notNull(scorelist) && scorelist.size() > 0)
		//
		// }// if (AssertUtils.notNull(mycomendList) && mycomendList.size() > 0)

		return mycomendList;
	}

	/**
	 * 获得积分兑换的列表
	 * 
	 * @author xiaoqian
	 * @param id
	 * @return 2014-3-7上午10:56:52
	 */
	public List<Map<String, Object>> selectexchangeRecordBymemberID(int memberID) {
		Map<String, Object> map = new HashMap<String, Object>();

		map.put("member_id", memberID);

		List<Map<String, Object>> list = daoHelper.queryForList(
				"com.zhixun.mobile.member.selectexchangeRecordBymemberID", map);
		return list;
	}

	// --------------------------------优惠券---------------------------------------------

	public List<Map<String, Object>> selectecouponConfigByuserID(int userID) {
		Map<String, Object> map = new HashMap<String, Object>();

		map.put("user_id", userID);

		List<Map<String, Object>> list = daoHelper.queryForList(
				"com.zhixun.mobile.member.selectecouponConfigByuserID", map);
		return list;
	}

	// --------------------------------优惠券---------------------------------------------

	/**
	 * 获得积分兑换的列表
	 * 
	 * @author songyan
	 * @param id
	 * @return 2014-3-7上午10:56:52
	 */
	public List<Map<String, Object>> selectcouponBymemberID(int memberID) {
		Map<String, Object> map = new HashMap<String, Object>();

		map.put("member_id", memberID);

		List<Map<String, Object>> list = daoHelper.queryForList(
				"com.zhixun.mobile.member.selectcouponBymemberID", map);
		return list;
	}

	/**
	 * 根据商家ID查询抽奖商品列表
	 * 
	 * @author songyan
	 * @param id
	 * @return 2014-3-7上午10:56:52
	 */
	public List<Lottery> selectlotteryByuserId(int userID) {
		Map<String, Object> map = new HashMap<String, Object>();

		map.put("user_id", userID);

		List<Lottery> list = daoHelper.queryForList(
				"com.zhixun.mobile.member.selectlotteryByuserId", map);

		if (AssertUtils.notNull(list)) {
			if (list.size() < 1) {
				list = null;
			}
		}

		return list;
	}

	/**
	 * 查询我的物品
	 * 
	 * @author songyan
	 * @param id
	 * @return 2014-3-7上午10:56:52
	 */
	public List<Map<String, Object>> selectmyGoodsBymemberID(Long member_id) {
		Map<String, Object> map = new HashMap<String, Object>();

		map.put("member_id", member_id);

		List<Map<String, Object>> list = daoHelper.queryForList(
				"com.zhixun.mobile.member.selectmyGoodsBymemberID", map);
		return list;
	}

	/**
	 * 放弃我的物品
	 * 
	 * @author songyan
	 * @param id
	 * @return 2014-3-7上午10:56:52
	 */
	public void deleteMyGoods(Long id, String type) {

		Map<String, Object> map = new HashMap<String, Object>();

		map.put("id", id);
		map.put("updateTime", new Date());
		map.put("remark", "用户放弃奖品");

		if ("lottery".equals(type)) {
			daoHelper.update("com.zhixun.mobile.member.deleteLotteryRecord",
					map);
		}
		if ("exchange".equals(type)) {
			daoHelper.update("com.zhixun.mobile.member.deleteExchangeRecord",
					map);
		}
		if ("coupon".equals(type)) {
			daoHelper.update("com.zhixun.mobile.member.deleteCoupon", map);
		}
		if ("seckill".equals(type)) {
			daoHelper.update("com.zhixun.mobile.member.deleteSeckillGainer",
					map);
		}

	}

	/**
	 * 查询我的消费
	 * 
	 * @author songyan
	 * @param id
	 * @return 2014-3-7上午10:56:52
	 */
	public List<Map<String, Object>> selectmyConsumeBymemberID(Long member_id) {
		Map<String, Object> map = new HashMap<String, Object>();

		map.put("member_id", member_id);

		List<Map<String, Object>> list = daoHelper.queryForList(
				"com.zhixun.mobile.member.selectmyConsumeBymemberID", map);
		return list;
	}

	/**
	 * 修改个人信息
	 * 
	 * @author songyan
	 * @param id
	 * @return 2014-3-7上午10:56:52
	 */
	public void updateMemberInformation(Map<String, Object> map) {

		daoHelper.update("com.zhixun.mobile.member.updateMemberInformation",
				map);

	}

	/**
	 * 添加用户积分表
	 * 
	 * @author songyan
	 * @param id
	 * @return 2014-3-7上午10:56:52
	 */
	public void insertMemberScore(Map<String, Object> map) {

		daoHelper.insert("com.zhixun.mobile.member.insertMemberScore", map);

	}

	/**
	 * 根据会员ID，兑换ID，查询兑换记录表数量
	 * 
	 * @author songyan
	 * @param id
	 * @return 2014-3-7上午10:56:52
	 */
	public Integer selectExchangeRecordByMemberIdAndExchangeId(
			Map<String, Object> map) {

		Integer integer = (Integer) daoHelper
				.queryForObject(
						"com.zhixun.mobile.member.selectExchangeRecordByMemberIdAndExchangeId",
						map);

		return integer;

	}

	// 优惠券条件查询
	
	
	
	// --------------------------------优惠券条件查询---------------------------------------------

		public List<Map<String, Object>> selectecouponConfigByArgs(Map<String, Object> map) {
		

			

			List<Map<String, Object>> list = daoHelper.queryForList(
					"com.zhixun.mobile.member.selectecouponConfigByArgs", map);
			return list;
		}

		// --------------------------------优惠券查询---------------------------------------------

}
